package com.wyg.demo.service.impl;

import com.wyg.demo.common.utils.CsvUtil;
import com.wyg.demo.component.CsvOperation;
import com.wyg.demo.domain.LongDistance;
import com.wyg.demo.mapper.LongDistanceMapper;
import com.wyg.demo.service.LongDistanceService;
import com.wyg.demo.component.FileOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ygWen
 * @Description:
 * @date 2021/12/29
 */
@Slf4j
@Service
public class LongDistanceServiceImpl implements LongDistanceService  {
    
    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    
    @Autowired
    private LongDistanceMapper longDistanceMapper;

    @Autowired
    private CsvOperation csvOperation;
    
    @Autowired
    private FileOperation fileOperation;

    @Value(value = "${trackerPath}")
    private String trackerPath;

    /**
     * list集合批量插入
     * @param list
     */
    @Override
    public void save(List<LongDistance> list) {
        try(SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
            LongDistanceMapper mapper = sqlSession.getMapper(LongDistanceMapper.class);
            for (LongDistance longDistance : list) {
                mapper.insert(longDistance);
            }
            sqlSession.commit();
        }
    }

    /**
     * 使用truncate清空表
     */
    @Override
    public void clearTab() {
        longDistanceMapper.truncateTable();
    }

    /**
     * 将指定目录下的所有csv文件中的数据导入到数据库中
     * @param dirPath csv文件所在目录路径
     * @throws IOException 
     */
    @Override
    public void csvInsert(String dirPath) throws IOException {
        LongDistance longDistance = new LongDistance();
        Class<LongDistance> myBeanClass = LongDistance.class;
        //String dirPath = "E:\\Study\\csvIntoMysql\\long";
        List<String> csvFileOnDir = fileOperation.getCsvFileOnDir(dirPath);
        for (String csvFilePath : csvFileOnDir) {
            if (!StringUtils.isEmpty(csvFilePath)){
                List<String> lineList = fileOperation.readFile(trackerPath);
                if (lineList.contains(csvFilePath)){
                    continue;
                } else {
                    log.info("Next operation csvFile: {}", csvFilePath);
                    List<LongDistance> longDistanceList = csvOperation.csvToBean(longDistance, myBeanClass, csvFilePath);
                    save(longDistanceList);
                    fileOperation.writeFile(trackerPath, csvFilePath);
                }
            }
        }
    }

    @Override
    public byte[] exportCsv() {
        List<LongDistance> distanceList = longDistanceMapper.selectAll();
        List<String[]> stringList = new ArrayList<>();
        for (LongDistance longDistance : distanceList) {
            String[] strings = new String[7];
            strings[0] = longDistance.getDate();
            strings[1] = longDistance.getFromarea();
            strings[2] = longDistance.getToarea();
            strings[3] = longDistance.getAvgDistance();
            strings[4] = longDistance.getPopnum();
            strings[5] = longDistance.getFlag();
            strings[6] = longDistance.getDatatime();
            stringList.add(strings);
        }
        String[] header = {"date", "fromarea", "toarea", "avg_distance", "popnum", "flag", "datatime"};
        return CsvUtil.writeCsvAfterToBytes(header, stringList);
    }
}
